package cn.edu.lnpu.manage.cms.controller;

import cn.edu.lnpu.common.web.BaseController;
import cn.edu.lnpu.manage.cms.service.PageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Api(value = "页面预览接口",description = "页面预览接口")
@Controller
public class CmsPagePreviewController extends BaseController {

    @Autowired
    private PageService pageService;

    @ApiOperation("页面预览")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageId",value = "页面ID",defaultValue = "5a795ac7dd573c04508f3a56", required=true,paramType="path")
    })
    @GetMapping(value="/cms/preview/{pageId}")
    public void preview(@PathVariable("pageId") String pageId){
        String pageHtml = pageService.getPageHtml(pageId);
        if(StringUtils.isNotEmpty(pageHtml)){
            try {
                ServletOutputStream outputStream = response.getOutputStream();
                response.setHeader("Content-type","text/html;charset=utf-8");
                outputStream.write(pageHtml.getBytes(StandardCharsets.UTF_8));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
